/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.post.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.post.domain.Post;
import me.zhengjie.post.domain.vo.PostQueryCriteria;
import me.zhengjie.post.service.PostService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author hwl
* @date 2024-12-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "post管理")
@RequestMapping("/api/post")
public class PostController {

    private final PostService postService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportPost(HttpServletResponse response, PostQueryCriteria criteria) throws IOException {
        postService.download(postService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询post")
    @ApiOperation("查询post")
    @AnonymousAccess
    public ResponseEntity<PageResult<Post>> queryPost(PostQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(postService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/paged")
    @Log("分页查询post")
    @ApiOperation("分页查询post")
    @AnonymousAccess
    public ResponseEntity<PageResult<Post>> pagedQueryPost(PostQueryCriteria criteria,
                                                           @RequestParam(defaultValue = "1") int current,
                                                           @RequestParam(defaultValue = "20") int size  ){
        Page<Object> page = new Page<>(current,size);
        return new ResponseEntity<>(postService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增post")
    @ApiOperation("新增post")
    @AnonymousAccess
    public Integer createPost(@Validated @RequestBody Post resources){
        return postService.create(resources);
    }

    @PutMapping
    @Log("修改post")
    @ApiOperation("修改post")
    @AnonymousAccess
    public ResponseEntity<Object> updatePost(@Validated @RequestBody Post resources){
        postService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除post")
    @ApiOperation("删除post")
    @AnonymousAccess
    public ResponseEntity<Object> deletePost(@RequestBody List<Integer> ids) {
        postService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @GetMapping("/my")
    @Log("查询我的post")
    @ApiOperation("查询我的post")
    @AnonymousAccess
    public List<Post> queryMyPost(@RequestParam String userName){

        return postService.getMyPost(userName);
    }
    @GetMapping("/favorite")
    @Log("查询我收藏的post")
    @ApiOperation("查询我收藏的post")
    @AnonymousAccess
    public List<Post> queryMyFavoritePost(@RequestParam String userName){
        return postService.getMyFavoritePost(userName);
    }

    @GetMapping("/getById")
    @Log("根据主键查询post")
    @ApiOperation("根据主键查询post")
    @AnonymousAccess
    public Post getPostById(@RequestParam Integer postId){
        return postService.getPostById(postId);
    }

    @GetMapping("/getPoemExplanation")
    @Log("根据主键查询post")
    @ApiOperation("根据主键查询post")
    @AnonymousAccess
    public List<Post> getPoemExplanation(){

        return postService.getPoemExplanation();
    }

    @GetMapping("/getCommunication")
    @Log("根据主键查询post")
    @ApiOperation("根据主键查询post")
    @AnonymousAccess
    public List<Post> getCommunication(){

        return postService.getCommunication();
    }
}